import os
from win32com import client
import glob
import docx
from docx.shared import Inches
def re_str(file, save_path, old, new):
    if not os.path.isdir(save_path):
        os.mkd
    doc_open = ''
    try:
        doc_open = client.Dispatch('kwps.Application')  #调用程序接口,如果是word请使用: Word.Application
        print(f"使用接口: WPS")
    except:
        doc_open = client.Dispatch('Word.Application')
        print("使用接口: Word")
    doc = doc_open.Documents.Open(file)
    doc_open.Selection.Find.ClearFormatting()
    doc_open.Selection.Find.Replacement.ClearFormatting()
    doc_open.Selection.Find.Execute(old, False, False, False, False, False, True, 1, True, new, 2 )
    doc.SaveAs(os.path.join(save_path, os.path.basename(file)), 16)
    doc.Close()
    doc_open.Quit()
    print('完成')

    print(doc_open)
    print(save, old, new, file)


re_str(file='3/1.doc', save_path='3', old='刘', new='林')